"""
联系人数据模型
"""

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Index
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base


class Contact(Base):
    """联系人模型"""
    __tablename__ = "contacts"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, comment="姓名")
    email = Column(String(255), nullable=False, comment="邮箱地址")
    phone = Column(String(50), nullable=True, comment="电话号码")
    company = Column(String(200), nullable=True, comment="公司名称")
    department = Column(String(100), nullable=True, comment="部门")
    position = Column(String(100), nullable=True, comment="职位")

    # 关联字段
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")

    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    user = relationship("User", back_populates="contacts")

    # 索引
    __table_args__ = (
        Index('idx_contacts_user_email', 'user_id', 'email'),
        Index('idx_contacts_user_name', 'user_id', 'name'),
        Index('idx_contacts_company', 'company'),
        Index('idx_contacts_created_at', 'created_at'),
    )

    def __repr__(self):
        return f"<Contact(id={self.id}, name='{self.name}', email='{self.email}')>"


class ContactGroup(Base):
    """联系人分组模型"""
    __tablename__ = "contact_groups"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, comment="分组名称")
    description = Column(Text, nullable=True, comment="分组描述")

    # 关联字段
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")

    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    user = relationship("User", back_populates="contact_groups")
    contact_group_members = relationship("ContactGroupMember", back_populates="group", cascade="all, delete-orphan")

    # 索引
    __table_args__ = (
        Index('idx_contact_groups_user_name', 'user_id', 'name'),
    )

    def __repr__(self):
        return f"<ContactGroup(id={self.id}, name='{self.name}')>"


class ContactGroupMember(Base):
    """联系人分组成员关联模型"""
    __tablename__ = "contact_group_members"

    id = Column(Integer, primary_key=True, index=True)

    # 关联字段
    contact_id = Column(Integer, ForeignKey("contacts.id"), nullable=False, comment="联系人ID")
    group_id = Column(Integer, ForeignKey("contact_groups.id"), nullable=False, comment="分组ID")

    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")

    # 关系
    contact = relationship("Contact")
    group = relationship("ContactGroup", back_populates="contact_group_members")

    # 索引
    __table_args__ = (
        Index('idx_contact_group_members_contact', 'contact_id'),
        Index('idx_contact_group_members_group', 'group_id'),
        Index('idx_contact_group_members_unique', 'contact_id', 'group_id', unique=True),
    )

    def __repr__(self):
        return f"<ContactGroupMember(contact_id={self.contact_id}, group_id={self.group_id})>"
